﻿using System;
using System.IO;
using System.Reflection;
using Flagwind.Plugins;

namespace Flagwind.Terminals.Plugins
{
	public class ApplicationContext : Flagwind.Plugins.PluginApplicationContext
	{
		#region 静态变量

		public new readonly static ApplicationContext Current = new ApplicationContext();

		#endregion

		#region 成员字段

		private Flagwind.Options.Configuration.OptionConfiguration _configuration;

		#endregion

		#region 私有构造

		private ApplicationContext() : base("Flagwind.Terminals.Plugins")
		{
			Flagwind.ComponentModel.ApplicationContextBase.Current = this;
		}

		#endregion

		#region 重写方法

		public override Flagwind.Options.Configuration.OptionConfiguration Configuration
		{
			get
			{
				if(_configuration == null)
				{
					string filePaht = Path.Combine(this.ApplicationDirectory, Assembly.GetEntryAssembly().GetName().Name) + ".option";

					if(File.Exists(filePaht))
					{
						_configuration = Options.Configuration.OptionConfiguration.Load(filePaht);
					}
					else
					{
						_configuration = new Options.Configuration.OptionConfiguration(filePaht);
					}
				}

				return _configuration;
			}
		}

		protected override IWorkbenchBase CreateWorkbench(string[] args)
		{
			PluginTreeNode node = this.PluginContext.PluginTree.Find(this.PluginContext.Settings.WorkbenchPath);

			if(node != null && node.NodeType == PluginTreeNodeType.Builtin)
			{
				return base.CreateWorkbench(args);
			}

			return new Workbench(this);
		}

		#endregion
	}
}